﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using XiaoGang.Soft.XGWMS.COM;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.DAL_SQL;

namespace XiaoGang.Soft.XGWMS.BLL
{
    public class BLL_M009_STORAGE_INPUT
    {
        XGWMS.DAL_SQL.DAL_M009_STORAGE_INPUT dal_M009_STORAGE_INPUT =  new XiaoGang.Soft.XGWMS.DAL_SQL.DAL_M009_STORAGE_INPUT();
        XGWMS.DAL_SQL.DAL_M010_BILL dal_M010_BILL = new XiaoGang.Soft.XGWMS.DAL_SQL.DAL_M010_BILL();
        private readonly XiaoGang.Soft.XGWMS.DAL_SQL.DAL_ComProcess dal_ComProcess = new XiaoGang.Soft.XGWMS.DAL_SQL.DAL_ComProcess();
        private readonly XiaoGang.Soft.XGWMS.BLL.BLL_ComProcess bll_ComProcess = new BLL_ComProcess();
        
        /// <summary>	
        /// <p><b>方法名称</b>InitComBoxProcess</p>	
        /// <p><b>概要説明  :</b>仓库编号的初始化</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type="ReturnValue">仓库编号和仓库名称表</returns>
        public ReturnValue InitComBoxProcess()
        {
            return dal_M009_STORAGE_INPUT.Init_Combox();
        }

        /// <summary>	
        /// <p><b>方法名称</b>GetSingleGoods</p>	
        /// <p><b>概要説明  :</b>取得一條商品信息</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/09</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="model">检索条件</param>
        /// <returns type="ReturnValue">单条商品信息</returns>
        public ReturnValue GetSingleGoods(MOD_M009_STORAGE_INPUT model)
        {
            return dal_M009_STORAGE_INPUT.GetSingleGoods(model);
        }

        /// <summary>	
        /// <p><b>方法名称</b>GetBillID</p>	
        /// <p><b>概要説明  :</b>取得货单番号</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/09</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <param name="model">采番条件</param>
        /// <returns type="string">番号</returns>
        public string GetBillID(MOD_M010_BILL model)
        {   
            #region 採番

            //ReturnValue returnValue;
            //MOD_M011_GET_ID modGetID = new MOD_M011_GET_ID();
            //modGetID.UPDATER_ID = model.UPDATER_ID;
            //modGetID.TERMINAL_CD = model.TERMINAL_CD;
            //modGetID.GETID_DIV = "05";
            //modGetID.FISCAL_YEAR = System.DateTime.Now.Year.ToString("####");
            //returnValue = bll_ComProcess.GetSlipID(modGetID);
            //if (!returnValue.ErrorFlag)
            //{
            //    return returnValue.ErrorID;
            //}
            //return returnValue.ResultDataSet.Tables[0].Rows[0]["SLIPID"].ToString().Trim();

            string UniqueNo = string.Empty;
            if (!DAL_ComProcess.GetUniqueNo(model.UPDATER_ID
                , model.TERMINAL_CD
                , "05"
                , System.DateTime.Now.Year.ToString("####")
                , out UniqueNo))
            {
                return "E11003";
            }
            return  UniqueNo;

            #endregion
        }        

        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>货单的插入</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">货单信息</param>	
        /// <returns type="string">执行结果信息</returns>
        public string InsertProcess(MODEL.MOD_M009_STORAGE_INPUT model)
        {
            #region 採番

            //MOD_M011_GET_ID modGetID = new MOD_M011_GET_ID();
            //modGetID.UPDATER_ID = model.UPDATER_ID;
            //modGetID.TERMINAL_CD = model.TERMINAL_CD;
            //modGetID.GETID_DIV = "04";
            //modGetID.FISCAL_YEAR = System.DateTime.Now.Year.ToString("####");
            //ReturnValue returnValue = bll_ComProcess.GetSlipID(modGetID);
            //if (!returnValue.ErrorFlag)
            //{
            //    return returnValue.ErrorID;
            //}
            //model.STORAGE_ID = returnValue.ResultDataSet.Tables[0].Rows[0]["SLIPID"].ToString().Trim();

            string UniqueNo = string.Empty;
            if (!DAL_ComProcess.GetUniqueNo(model.UPDATER_ID
                , model.TERMINAL_CD
                , "04"
                , System.DateTime.Now.Year.ToString("####")
                , out UniqueNo))
            {
                return "E11003";
            }
            model.STORAGE_ID = UniqueNo;

            #endregion

            model.TRANS_STATE = "1";
            ReturnValue returnValue = dal_M009_STORAGE_INPUT.InsertProcess(model);
            if (returnValue.IsSuccess == false)
            {
                return returnValue.ErrorID;
            }
            return "0";
        }        

        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>库存的插入</p>	
        /// <p><b>作成者	:</b>万岗</p>	
        /// <p><b>作成日	:</b>2008/09/03</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" param="strConstrolName">商品信息</param>	
        /// <returns type="string">执行结果信息</returns>
        public string InsertProcess(MODEL.MOD_M010_BILL model)
        {
            ReturnValue returnValue;
            returnValue = dal_M010_BILL.InsertProcess(model);
            if (returnValue.IsSuccess == false)
            {
                return returnValue.ErrorID;
            }
            return "0";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValue AddUpdateStorage(MODEL.MOD_M009_STORAGE_INPUT model)
        {
            ReturnValue returnValue = dal_M009_STORAGE_INPUT.Get_billIdGoodsId(model.GOODS_ID, model.BILL_ID);
            if(!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if(returnValue.ResultDataSet.Tables[0].Rows.Count>0)
            {
                returnValue.IsSuccess = false;
                returnValue.ErrorID = "I11004";//该商品已经过盘库，若想改变其数量请在新盘库中进行！
                return returnValue;
                //returnValue=dal_M009_STORAGE_INPUT.Update_StorageUnits(model);
            }
            else
            {
                string strReTurn=InsertProcess(model);
                if (strReTurn != "0")
                {
                    returnValue.IsSuccess = false;
                    returnValue.ErrorID = strReTurn;
                }
            }
            return returnValue;
        }
    }
}
